package com.edu.gcu.manager.controller;

import com.edu.gcu.manager.service.IIndentService;
import com.edu.gcu.system.common.Const;
import com.edu.gcu.system.common.ResponseCode;
import com.edu.gcu.system.common.ServerResponse;
import com.edu.gcu.system.pojo.Admin;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author polo
 * @version V1.0
 * @description: 后台订单控制器
 * @date 2018-02-20-12:09
 */

@Controller
@RequestMapping("/manage/indent")
public class IndentManageController {

    private static Logger logger = LoggerFactory.getLogger(IndentManageController.class);

    @Autowired
    private IIndentService iIndentService;

    private ServerResponse returnPublic(HttpSession session, ServerResponse response) {
        Admin currentAdmin = (Admin) session.getAttribute(Const.CURRENT_ADMIN);
        if (currentAdmin == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),
                    ResponseCode.NEED_LOGIN.getDesc());
        }
        return response;
    }

    /**
     * @description 其他订单列表
     * @author polo
     */
    @ResponseBody
    @RequestMapping("otherList.do")
    public ServerResponse otherList(@RequestParam(value = "productId", defaultValue = "") String productId,
                                    @RequestParam(value = "consignerId", defaultValue = "") String consignerId,
                                    @RequestParam(value = "consigneeId", defaultValue = "") String consigneeId,
                                    @RequestParam(value = "status", defaultValue = "") Integer status,
                                    @RequestParam(value = "orderBy", required = false) String orderBy,
                                    @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                    HttpSession session) {
        if (StringUtils.equals(consignerId, Const.ADMIN)) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ILLEGAL_ARGUMENT.getCode(), ResponseCode
                    .ILLEGAL_ARGUMENT.getDesc());
        }
        return returnPublic(session, iIndentService.otherList(productId, consignerId, consigneeId, status, orderBy,
                pageNum, pageSize));
    }

    /**
     * @description 一级经销商订单列表
     * @author polo
     */
    @ResponseBody
    @RequestMapping("list.do")
    public ServerResponse list(@RequestParam(value = "productId", defaultValue = "") String productId,
                               @RequestParam(value = "consigneeId", defaultValue = "") String consigneeId,
                               @RequestParam(value = "status", defaultValue = "") Integer status,
                               @RequestParam(value = "orderBy", required = false) String orderBy,
                               @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                               HttpSession session) {
        return returnPublic(session, iIndentService.otherList(productId, "admin", consigneeId, status, orderBy,
                pageNum, pageSize));
    }

    /**
     * @description 订单详情
     * @author polo
     */
    @ResponseBody
    @RequestMapping("detail.do")
    public ServerResponse detail(@RequestParam(value = "id") String id,
                                 HttpSession session) {
        return returnPublic(session, iIndentService.detail(id));
    }

    /**
     * @description 生成出货单
     * @author polo
     */
    @ResponseBody
    @RequestMapping("send.do")
    public ServerResponse send(@RequestParam(value = "id") String id,
                               HttpSession session) {
        return returnPublic(session, iIndentService.send(id));
    }

}
